package com.qishanor.framework.util;

import com.qishanor.framework.constant.CommonConstant;
import lombok.*;
import lombok.experimental.Accessors;
import java.io.Serializable;

/**
 * 响应信息主体
 *
 */
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @Getter
    @Setter
    private int code;

    @Getter
    @Setter
    private String msg;

    @Getter
    @Setter
    private T data;

    public static <T> R<T> ok() {
        return restResult(CommonConstant.SUCCESS, null, null);
    }

    public static <T> R<T> ok(T data) {
        return restResult(CommonConstant.SUCCESS, null, data);
    }

    public static <T> R<T> ok(String msg, T data) {
        return restResult(CommonConstant.SUCCESS, msg, data);
    }

    public static <T> R<T> failed() {
        return restResult(CommonConstant.FAIL, null, null);
    }

    public static <T> R<T> failed(String msg) {
        return restResult(CommonConstant.FAIL, msg, null);
    }

    public static <T> R<T> failed(T data) {
        return restResult(CommonConstant.FAIL, null, data);
    }

    public static <T> R<T> failed(String msg, T data) {
        return restResult(CommonConstant.FAIL, msg, data);
    }

    private static <T> R<T> restResult(int code, String msg, T data) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

}
